/**
 * Filename: GLButton.js  also contains GALayer
 * Author: Sajid Hussain
 * Date Created: 2011-02-08
 * Date Updated: 2011-02-10
 * Date Updated: 2011-05-13
 */
var GALayer = exports.GALayer = Core.MessageListener.subclass({

    classname: 'GALayer',

    initialize: function() {
        this._node = new GL2.Node();
    },

    getNode: function() {
        return this._node;
    },

    setPosition: function(x, y) {
        this._node.setPosition(x, y);
    },

    getPosition: function() {
        return this._node.getPosition();
    },

    setDepth: function(depth) {
        this._node.setDepth(depth);
    },

    addChild: function(aNode) {
        if (aNode) {
            this._node.addChild(aNode);
        }
    },

    destroy: function($super) {
        // $super();
        this._node.destroy();
        //alert('destroyed')
    }
});


exports.GLButton = GALayer.subclass({

    classname: 'GLButton',

    initialize: function(path1, path2, sizeX, sizeY, callBackFunc, node, depth ,btnText, normalTextColor,highlitedTextColor,textLableSize,fontSize) {
    
        this._ButtonEnabled=true;
        this._DisabledImagePath=null;
        this.buttonText = null;
        
        if(depth)
		{
			this.setDepth(depth);
		}
        this._button = GLFactory.getSprite(null, depth, path1, new Core.Size(sizeX, sizeY), [0.5, 0.5]);
        this.addChild(this._button);
        if(btnText)
        {
        	
        	if(textLableSize)
        	{
             	this.buttonText = GLFactory.getText([-28, 0],6, btnText, textLableSize,(fontSize) ?fontSize : 20,null, null,GL2.Text.OverflowMode.ReduceFontSize,[0.5,0.5]);
        		
        	}
        	else{
        		this.buttonText = GLFactory.getText([TCUtil.getAdjustedX(0),TCUtil.getAdjustedY(0)],6, btnText, [sizeX-TCUtil.getAdjustedX(6), sizeY], (fontSize) ?fontSize : 20,null, null,GL2.Text.OverflowMode.ReduceFontSize,[0.5,0.5]);
        	}
        	
        	this.buttonText.setHorizontalAlign(GL2.Text.HorizontalAlign.Center);
			this._button.addChild(this.buttonText);
        }
        if(normalTextColor)
        {
         this._normalTextColor = normalTextColor;
		 this.buttonText.setColor(normalTextColor);
        	
        }
        if(highlitedTextColor)
        {
         this._highlitedTextColor = highlitedTextColor;	
        }
        
        this._target = GLFactory.getTouchTarget(this.getPosition(), [sizeX, sizeY]);
        this._target.setAnchor(0.5, 0.5);
        this._target.setDepth(depth);
        this.addChild(this._target);

        this._target.getTouchEmitter().addListener(this, this._onTouch,100);

        this._callBackFunc = callBackFunc;

        this._path1 = path1;
        this._path2 = path2;
        this._sizeX = sizeX;
        this._sizeY = sizeY;

        node.addChild(this.getNode());
    },

    _onTouch: function(touch) {
        switch (touch.getAction()) {
        case touch.Action.Start:
            	
       		 if(this._highlitedTextColor)
       		 {
       		  this.buttonText.setColor(this._highlitedTextColor);	
       		 }
       		if (this._path2){
            this._button.setImage(this._path2, [this._sizeX, this._sizeY]);
       		}
            return true;
        case touch.Action.End:
        	 if(this._normalTextColor)
       		 {
       		  this.buttonText.setColor(this._normalTextColor);	
       		 }
			if (this._path1){
            this._button.setImage(this._path1, [this._sizeX, this._sizeY]);
			}
            if(touch.getIsInside(this._target)) {
            	this._callBackFunc();
            }
            break;
        case touch.Action.Move:
            if(touch.getIsInside(this._target)){
            	if (this._path2){
            	this._button.setImage(this._path2, [this._sizeX, this._sizeY]);
            	}
            }
            else{
            	if (this._path1){
            	this._button.setImage(this._path1, [this._sizeX, this._sizeY]);
            	}
            }
            break;
        }
    },
    
    setButtonTextBelowButton: function()
    {
    	if(this.buttonText)
    	{
    		this.buttonText.setPosition(0, this._sizeY/2 + TCUtil.getAdjustedY(8));
    	}
    },
    
    getButton:function ()
    {
    	return this._button;
    },

    setIsEnabled: function(value)
    {
    	this._ButtonEnabled=value;
    	this._target.setTouchable(this._ButtonEnabled);
    	this.setDisabledImage(value);
    },
    
    setDisabledImage: function(value)
    {
    	
    	if(value)
    	{
    		this._button.setImage(this._path1, new Core.Size(this._sizeX, this._sizeY), [0.5, 0.5]);
	    }
    	else if(this._DisabledImagePath!=null)
    	{
			this._button.setImage(this._DisabledImagePath, new Core.Size(this._sizeX, this._sizeY), [0.5, 0.5]);
    	}
    },
    
    setDisabledImagePath: function(path)
    {
    	this._DisabledImagePath=path;
    },
   
    
    destroy: function($super) {
        $super();
        this._target.getTouchEmitter().removeListener(this, this._onTouch);
		if(this._button)
		{
			this._button.destroy();
			this._button = null;
		}
		if(this.buttonText)
		{
			this.buttonText.destroy();
			this.buttonText = null;
		}
    }

});

